/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.tcp;

import java.io.IOException;
import java.lang.reflect.Field;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;

import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;
import com.zxy.common.ParamName_InterFace;

public class PushCallback implements MqttCallback
{
	public void connectionLost(Throwable cause)
	{
		// MqttHandler mh = new MqttHandler();
		// mh.MQTTconnect();
	}

	public void deliveryComplete(IMqttDeliveryToken token)
	{
		// System.out.println("deliveryComplete---------" + token.isComplete());
	}

	public void messageArrived(String topic, MqttMessage message) throws Exception
	{
		// subscribe后得到的消息会执行到这里面 message.paload
		// System.out.println("接收客户端消息主题 : " + topic);
		// System.out.println("接收客户端消息Qos : " + message.getQos());
		// System.out.println("接收客户端消息内容 : " + new
		// String(message.getPayload()));
		// String strResult = new String(message.getPayload(),Com_Para.U_CODE);
		StringBuilder strResult = new StringBuilder();
		strResult.append(new String(message.getPayload(),Com_Para.U_CODE));

		if(!Com_Para.MQTTREFLECT_IN_CLASS.equals(""))
		{
			try
			{
				Class<?> objC = Class.forName(Com_Para.MQTTREFLECT_IN_CLASS);
				ParamName_InterFace pni = (ParamName_InterFace) objC.newInstance();
				for(Field field: pni.getClass().getDeclaredFields())
				{
					field.setAccessible(true);
					if(field.getName().equals("strResult"))
						field.set(pni,strResult);
					else if(field.getName().equals("strIP"))
						field.set(pni,"0.0.0.0");
					else if(field.getName().equals("bytesResult"))
						field.set(pni,message.getPayload());
					else if(field.getName().equals("param_value1"))
						field.set(pni,topic);
				}
				pni.init_start();
				for(Field field: pni.getClass().getDeclaredFields())
				{
					if(field.getName().equals("strSend"))
					{
						Object fieldGetName = Com_Fun.getFieldValueByName(field.getName(),pni);
						if(!fieldGetName.toString().equals(""))
						{
							// String strTemSend = fieldGetName.toString();
						}
					}else if(field.getName().equals("strResult"))
					{
						Object fieldGetName = Com_Fun.getFieldValueByName(field.getName(),pni);
						if(!fieldGetName.toString().equals(""))
						{
							// String strTemResult = fieldGetName.toString();
						}
					}else if(field.getName().equals("strContinue"))
					{
						Object fieldGetName = Com_Fun.getFieldValueByName(field.getName(),pni);
						if(!fieldGetName.toString().equals(""))
						{
							// temContinue = fieldGetName.toString();
						}
					}
				}
			}
			catch(Exception e)
			{
				Com_Fun.debug_in(e,this.toString(),Com_Para.MQTTREFLECT_IN_CLASS + "作为MQTT接收数据拦截器获取失败!");
			}
		}
	}
}